//
//  MysettingVC.swift
//  Venus
//
//  Created by qurongxiang on 2018/9/8.
//  Copyright © 2018年 zhaowang. All rights reserved.
//

import UIKit
import Kingfisher

private let cellID = "cellID"

class MysettingVC: BaseViewController {
    
    
    
    fileprivate lazy var tableView : UITableView = {
        let tableView = UITableView(frame: CGRect.zero)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
        return tableView
    }()
    
    
    fileprivate var modelArr:[String] = ["清除缓存","关于我们","退出账号"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    
    
}

// MARK:- 代理
extension MysettingVC:UITableViewDelegate , UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return modelArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView .dequeueReusableCell(withIdentifier: cellID, for: indexPath)
        cell.backgroundColor = nC1
        cell.textLabel?.text = modelArr[indexPath.item]
        cell.textLabel?.font = UIFont.systemFont(ofSize: 14)
        cell.backgroundColor = UIColor.white
        
        cell.contentView.viewWithTag(1024)?.removeFromSuperview()
        let lineView = UIView()
        cell.contentView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        lineView.backgroundColor = nC1
        lineView.tag = 1024
        
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.item == 0 {
            hudLoadMessage("清除缓存成功")
            let cache = KingfisherManager.shared.cache
            cache.clearDiskCache()//清除硬盘缓存
            cache.clearMemoryCache()//清理网络缓存
            cache.cleanExpiredDiskCache()//清理过期的，或者超过硬盘限制大小
        }
        if indexPath.item == 1 {
            let vc = DYWebViewController()
            vc.myUrl = "https://www.baidu.com"
            vc.myTitle = "关于我们"
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
        if indexPath.item == 2 {
            let alertController = UIAlertController(title: "系统提示",
                                                    message: "您确定要退出吗？", preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            let okAction = UIAlertAction(title: "好的", style: .default, handler: {
                action in
                UserInfo.shareInstance.setUserModel(model: UserModel())
                UIApplication.shared.keyWindow?.rootViewController = LogionViewController()
                
            })
            alertController.addAction(cancelAction)
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
}


extension MysettingVC {
    func setupUI() {
        self.title = "系统设置"
        view.backgroundColor = UIColor.white
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.top.equalTo(view).offset(LayoutCommon.navHeight)
            make.left.right.equalToSuperview()
            if #available(iOS 11.0, *) {
                make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
            } else {
                make.bottom.equalTo(view.snp.bottom)
            }
        }
    }
}
